package api

import (
	"getaway/basic/config"
	driver "getaway/basic/proto/driverproto"
	"getaway/handler/request"
	"getaway/handler/response"
	"github.com/gin-gonic/gin"
)

// AddRoute
// @Summary      添加线路
// @Description  添加线路
// @Tags         线路模块
// @Accept       json
// @Produce      json
// @Param        vehicle   body    request.AddRouteReq   true  "线路参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /vehicle/addRouter [post]
func AddRouter(c *gin.Context) {
	var req request.AddRouteReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	route, err := config.DriverClient.CreateRoute(c, &driver.CreateRouteReq{
		UserId:     req.UserId,
		DriverId:   int64(req.DriverId),
		RouteName:  req.RouteName,
		RouteCode:  req.RouteCode,
		StartPoint: req.StartPoint,
		EndPoint:   req.EndPoint,
		RouteTags:  req.RouteTags,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "添加线路成功", route.Id)
}

// AddSite
// @Summary      添加站点
// @Description  添加站点
// @Tags         线路模块
// @Accept       json
// @Produce      json
// @Param        driver   body   request.AddSiteReq   true  "线路参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /vehicle/addSite [post]
func AddSite(c *gin.Context) {
	var req request.AddSiteReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	site, err := config.DriverClient.CreateSite(c, &driver.CreateSiteReq{
		RouteId:     req.RouteId,
		SiteCode:    req.SiteCode,
		SiteName:    req.SiteName,
		Longitude:   float32(req.Longitude),
		Latitude:    float32(req.Latitude),
		ArrivalTime: req.ArriveTime,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "添加站点成功", site.Id)

}

// AddRideRecord
// @Summary      添加用户乘车
// @Description  添加用户乘车
// @Tags         线路模块
// @Accept       json
// @Produce      json
// @Param        vehicle   body   request.AddRideRecordReq   true  "线路参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /vehicle/addRideRecord [post]
func AddRideRecord(c *gin.Context) {
	var req request.AddRideRecordReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	rideRecord, err := config.DriverClient.CreateRideRecord(c, &driver.CreateRideRecordReq{
		UserId:     req.UserId,
		RouteId:    req.RouteId,
		SiteId:     req.SiteId,
		StartPoint: req.StartPoint,
		EndPoint:   req.EndPoint,
		Status:     req.Status,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "添加用户乘车成功", rideRecord.Id)
}

// GetRouter
// @Summary      线路展示
// @Description  线路展示
// @Tags         线路模块
// @Accept       json
// @Produce      json
// @Param        routeId   query   int64   true  "线路ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /vehicle/getRouter [get]
func GetRouter(c *gin.Context) {
	var req request.GetRouterReq
	if err := c.ShouldBindQuery(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	router, err := config.DriverClient.ListSite(c, &driver.ListSiteReq{})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "线路展示成功", router)
}
